package net.huashitong.ssydt.provider.msg.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.msg.entity.vo.*;
import net.huashitong.ssydt.provider.annotations.LoginRequired;
import net.huashitong.ssydt.provider.msg.service.MsgService;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.11.14
 */
@Api(value = "/api/msg/", tags = {"消息模块"})
@RestController
@RequestMapping("/api/msg/")
public class ApiMsgController {

    private MsgService msgService;

    @Autowired
    public ApiMsgController(MsgService msgService) {
        this.msgService = msgService;
    }

    @ApiOperation(value = "获取单条消息内容", notes = "获取单条消息内容", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/messages/{messageId}")
    public Result<MessageOutVO> getMessages(@ApiParam(value = "消息id", required = true) @PathVariable("messageId") Long messageId) {
        MessageOutVO vo = this.msgService.getMessages(messageId);
        return ResultUtils.getSuccessResults(vo);
    }

    @ApiOperation(value = "获取我的未读消息列表", notes = "获取我的未读消息列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/messages")
    public Result<MessageListOutVO> getMessagesList(@Valid MessageInVO messageInVO) {
        String userId = UserUtils.getUserId();
        MessageListOutVO vo = this.msgService.getMessagesList(userId, messageInVO);
        return ResultUtils.getSuccessResults(vo);
    }


    @ApiOperation(value = "标记为已读", notes = "标记为已读", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PutMapping("/messages/{messageId}")
    public Result<String> putMessages(@ApiParam(value = "消息id", required = true) @PathVariable("messageId") Long messageId) {
        String userId = UserUtils.getUserId();
        this.msgService.putMessages(userId, messageId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "标记为全部已读", notes = "标记为全部已读", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PutMapping("/messages")
    public Result<String> putMessagesAll(@RequestBody MessageReadInVO messageReadInVO) {
        String userId = UserUtils.getUserId();
        this.msgService.putMessagesAll(userId, messageReadInVO);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "获取推送设置", notes = "获取推送设置", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/remind-config")
    public Result<RemindConfigVO> getRemindConfig() {
        String userId = UserUtils.getUserId();
        RemindConfigVO vo = this.msgService.getRemindConfig(userId);
        return ResultUtils.getSuccessResults(vo);
    }


    @ApiOperation(value = "修改推送设置", notes = "修改推送设置", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PutMapping("/remind-config")
    public Result<String> putRemindConfig(@RequestBody RemindConfigVO remindConfigVO) {
        String userId = UserUtils.getUserId();
        this.msgService.putRemindConfig(userId, remindConfigVO);
        return ResultUtils.getSuccessResults();
    }
}
